package com.aier.admin.action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin.entity.AssignMessage;
import com.aier.admin.service.IAssignMessageService;
import com.aier.common.dao.ICommonDAO;
import com.aier.proxy.service.IProxyService;
import com.aier.user.common.UserFinalPageSize;

@Scope("prototype")
@Component("adminMessageAssignAction")
public class AdminMessageAssignAction extends AdminBaseAction {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    @Resource(name = "assignMessageService")
    private IAssignMessageService assignMessageService;
    //页数
    private int page;
    // 每页显示记录数
    private int page_size;
    // 总页数
    private int page_count;
    // 总数
    private int count;
    //错误码
    private int s;
    
    // 排序方式
    private String sord;
    // 排序字段
    private String sort;
    // 学校列表
    private List<AssignMessage> schoolsList;
    // 学校名称
    private String sname;
    // 学校所在省市编号
    private Integer cid;
    // 学校id
    private Integer sid;

    // 分配短信条数
    private Integer mcount;
    // 分配短信历史记录
    private List<AssignMessage> assignHistoryList;
    
    
    @Override
    public String execute(){
        return SUCCESS;
    }
    
     /**
      * 加载学校短信信息
      * @return
      */
    @SuppressWarnings("unchecked")
    public String searchSchool() {
        
        if (page <= 0) {
            page = 1;
        }
        int sortable=-1;
        if("cid".equals(sort)){
            sortable = ("asc".equals(sord))?IProxyService.ENTRANCE_SORTABLE_SN:IProxyService.ENTRANCE_SORTABLE_SN_DESC;
        }
        Map<String, Object> map = assignMessageService.serachSchoolList(sname,cid,page, UserFinalPageSize.PROXY_SCHOOLS_PAGESIZE,sortable);
        schoolsList =  (List<AssignMessage>) map.get(ICommonDAO.OBJ_LIST);
        if (null == schoolsList) {
            s = 0;
            return SUCCESS;
        }
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        s=1;
        return SUCCESS;
    }
    /**
     * 分配短信
     * @return
     */
    public String assignMessage() {
        Timestamp adate = new Timestamp(new Date().getTime());
        s = assignMessageService.insertAssign(sid, mcount, adate);
        return SUCCESS;
    }
    
    /**
     * 查询学校分配短信的历史记录
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchAssignList() {
       
        if (page <= 0) {
            page = 1;
        }
        int sortable=-1;
        if("adate".equals(sort)){
            sortable = ("asc".equals(sord))?IProxyService.ENTRANCE_SORTABLE_SN:IProxyService.ENTRANCE_SORTABLE_SN_DESC;
        }
        Map<String, Object> map = assignMessageService.serachsearchAssignList(sid,page, UserFinalPageSize.PROXY_SCHOOLS_PAGESIZE,sortable);
        assignHistoryList =  (List<AssignMessage>) map.get(ICommonDAO.OBJ_LIST);
        if (null == schoolsList) {
            s = 0;
            return SUCCESS;
        }
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        s=1;
        return SUCCESS;
    }
    
    public int getPage() {
        return page;
    }


    public void setPage(int page) {
        this.page = page;
    }


    public int getPage_size() {
        return page_size;
    }


    public int getPage_count() {
        return page_count;
    }


    public int getCount() {
        return count;
    }


    public int getS() {
        return s;
    }


    public void setSord(String sord) {
        this.sord = sord;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }
    
    public List<AssignMessage> getSchoolsList() {
        return schoolsList;
    }

    public void setAssignMessageService(IAssignMessageService assignMessageService) {
        this.assignMessageService = assignMessageService;
    }
    
    public List<AssignMessage> getAssignHistoryList() {
        return assignHistoryList;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }
    public Integer getSid() {
        return sid;
    }

    public void setMcount(int mcount) {
        this.mcount = mcount;
    }

}
